﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace CodeTunnel.MvcUtilities.Components
{
    /// <summary>
    /// A DefaultModelBinder extension that checks the built-in DependencyResolver when model binding.
    /// </summary>
    public class DependencyResolvingModelBinder : DefaultModelBinder
    {
        /// <summary>
        /// An override that checks DependencyResolver when creating a model.
        /// </summary>
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            return DependencyResolver.Current.GetService(modelType) ?? base.CreateModel(controllerContext, bindingContext, modelType);
        }
    }
}
